﻿package mortal.game.view.autoArena.view
{
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.gengine.global.*;
    import com.greensock.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.utils.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.cd.*;
    import mortal.component.window.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.layer3D.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.autoArena.*;
    import mortal.game.view.autoArena.util.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.mainUI.roleAvatar.*;

    public class AutoArenaVsInfoSpr extends Window
    {
        private var _headIcon1:GBitmap;
        private var _roleLevel1:GTextFiled;
        private var _roleName1:GTextFiled;
        private var _roleCombat1:BitmapNumberText;
        private var _bloodBar1:BaseProgressBar;
        private var _buffPanel1:BuffPanel;
        private var _headIcon2:GBitmap;
        private var _roleLevel2:GTextFiled;
        private var _roleName2:GTextFiled;
        private var _roleCombat2:BitmapNumberText;
        private var _bloodBar2:BaseProgressBar;
        private var _buffPanel2:BuffPanel;
        private var _startNum:BitmapNumberText;
        private var _sectimeData:SecTimerViewData;
        private var _mask:GSprite;
        private var _self:EntityInfo;
        private var _other:EntityInfo;
        private var _frameTimer:FrameTimer;
        private var _isDrawMask:Boolean;
        private static var _instance:AutoArenaVsInfoSpr;

        public function AutoArenaVsInfoSpr()
        {
            this.layer = LayerManager.windowLayer;
            isForbidenDrag = true;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            _isAddClolseButton = false;
            super.createDisposedChildrenImpl();
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.AutoArenaVsInfo, 0, 0, this));
            this._headIcon1 = UIFactory.gBitmap("", 18, 5 - 20, this);
            this._roleCombat1 = UIFactory.bitmapNumberText(210 + 13, 10 - 2, "VipLevelNum.png", 17, 20, -7, this, 9);
            this._roleName1 = UIFactory.gTextField("", 95 + 16, 40 - 2, 125, 20, this);
            this._roleLevel1 = UIFactory.gTextField("", 66 + 14, 75, 28, 20, this);
            this._bloodBar1 = UICompomentPool.getUICompoment(BaseProgressBar);
            this._bloodBar1.setProgress(ImagesConst.AutoArenaBar1, true, 14, 1, 227, 13);
            this._bloodBar1.setLabel(BaseProgressBar.ProgressBarTextNone);
            this._bloodBar1.setValue(10000, 10000);
            this._bloodBar1.x = 85 + 4;
            this._bloodBar1.y = 58 + 2;
            this.addChild(this._bloodBar1);
            this._buffPanel1 = UICompomentPool.getUICompoment(BuffPanel);
            this._buffPanel1.createDisposedChildren();
            this._buffPanel1.x = 85 + 10 + 10;
            this._buffPanel1.y = 58 + 2 + 21 - 2;
            this.addChild(this._buffPanel1);
            this._headIcon2 = UIFactory.gBitmap("", 637 + 90, 5 - 20, this);
            this._headIcon2.scaleX = -1;
            this._roleCombat2 = UIFactory.bitmapNumberText(448 + 11, 10 - 2, "VipLevelNum.png", 17, 20, -7, this, 9);
            this._roleName2 = UIFactory.gTextField("", 548 + 2, 40 - 2, 125, 20, this);
            this._roleLevel2 = UIFactory.gTextField("", 636 + 15, 75, 28, 20, this);
            this._bloodBar2 = UICompomentPool.getUICompoment(BaseProgressBar);
            this._bloodBar2.setProgress(ImagesConst.AutoArenaBar1, true, 14, 1, 227, 13);
            this._bloodBar2.setLabel(BaseProgressBar.ProgressBarTextNone);
            this._bloodBar2.setValue(10000, 10000);
            this._bloodBar2.x = 403 + 2 + 256;
            this._bloodBar2.y = 58 + 2;
            this.addChild(this._bloodBar2);
            this._bloodBar2.scaleX = -1;
            this._buffPanel2 = UICompomentPool.getUICompoment(BuffPanel);
            this._buffPanel2.createDisposedChildren();
            this._buffPanel2.x = 523 + 26 + 95;
            this._buffPanel2.y = 58 + 2 + 21 - 2;
            this.addChild(this._buffPanel2);
            this._buffPanel2.scaleX = -1;
            this._startNum = UIFactory.bitmapNumberText(340 + 8, 230, "GuildCopyNumBig.png", 55, 77, 0, this, 9);
            this._startNum.text = "";
            this._sectimeData = new SecTimerViewData();
            this._sectimeData.setLeftTime(4);
            this._sectimeData.caller = this.onTimeChangeHandler;
            this._sectimeData.timeOutCaller = this.onTimeComplHandler;
            this._mask = UICompomentPool.getUICompoment(GSprite);
            this._startNum.addChild(this._mask);
            this.stageResize();
            return;
        }// end function

        private function addListeners() : void
        {
            if (this._self)
            {
                this._self.addEventListener(EntityInfoEventName.UpdateLife, this.updateSelfLife);
                this._self.addEventListener(EntityInfoEventName.UpdateBuffs, this.updateSelfBuffs);
            }
            if (this._other)
            {
                this._other.addEventListener(EntityInfoEventName.UpdateLife, this.updateOtherLife);
                this._other.addEventListener(EntityInfoEventName.UpdateBuffs, this.updateOtherBuffs);
            }
            return;
        }// end function

        private function removeListeners() : void
        {
            if (this._self)
            {
                this._self.removeEventListener(EntityInfoEventName.UpdateLife, this.updateSelfLife);
                this._self.removeEventListener(EntityInfoEventName.UpdateBuffs, this.updateSelfBuffs);
            }
            if (this._other)
            {
                this._other.removeEventListener(EntityInfoEventName.UpdateLife, this.updateOtherLife);
                this._other.removeEventListener(EntityInfoEventName.UpdateBuffs, this.updateOtherBuffs);
            }
            return;
        }// end function

        private function initRoleInfo() : void
        {
            var _loc_1:* = ThingUtil.entityUtil.entitysMap;
            if (Game.isSceneInit() && RolePlayer.instance.entityInfo && _loc_1.length == 2)
            {
                this.initInfo();
            }
            else
            {
                if (!this._frameTimer)
                {
                    this._frameTimer = new FrameTimer(1, int.MAX_VALUE, true);
                    this._frameTimer.addListener(TimerType.ENTERFRAME, this.onSceneInit);
                }
                this._frameTimer.start();
            }
            return;
        }// end function

        private function onSceneInit(param1:FrameTimer) : void
        {
            var _loc_2:* = ThingUtil.entityUtil.entitysMap;
            if (Game.isSceneInit() && RolePlayer.instance.entityInfo && _loc_2.length == 2)
            {
                if (this._frameTimer)
                {
                    this._frameTimer.stop();
                }
                this.initInfo();
            }
            return;
        }// end function

        private function initInfo() : void
        {
            var _loc_4:IEntity = null;
            this._self = Cache.instance.role.roleEntityInfo;
            this._headIcon1.bitmapData = GlobalClass.getBitmapData(AutoArenaUtil.getHeadIcon(this._self.entityInfo.career, this._self.entityInfo.sex));
            this._roleCombat1.text = this._self.entityInfo.combat + "";
            this._roleName1.text = this._self.entityInfo.name;
            this._roleLevel1.text = this._self.entityInfo.level + "";
            var _loc_1:* = RolePlayer.instance;
            var _loc_2:* = ThingUtil.entityUtil.entitysMap.allEntitys;
            var _loc_3:* = AutoArenaController.currChallengeEntityId;
            for each (_loc_4 in _loc_2)
            {
                
                if (_loc_4 is UserPlayer && !EntityUtil.isSelf(_loc_4.entityInfo.entityInfo.entityId))
                {
                    this._other = _loc_4.entityInfo;
                    this._headIcon2.bitmapData = GlobalClass.getBitmapData(AutoArenaUtil.getHeadIcon(this._other.entityInfo.career, this._other.entityInfo.sex));
                    this._roleCombat2.text = this._other.entityInfo.combat + "";
                    this._roleName2.text = this._other.entityInfo.name;
                    this._roleLevel2.text = this._other.entityInfo.level + "";
                    break;
                }
            }
            this.addListeners();
            return;
        }// end function

        private function updateSelfLife(event:Event) : void
        {
            this._bloodBar1.setValue(this._self.entityInfo.life, this._self.entityInfo.maxLife);
            return;
        }// end function

        private function updateOtherLife(event:Event) : void
        {
            this._bloodBar2.setValue(this._other.entityInfo.life, this._other.entityInfo.maxLife);
            return;
        }// end function

        private function updateSelfBuffs(event:Event) : void
        {
            this._buffPanel1.updateEntityBuffer(this._self.entityInfo.buffInfos);
            return;
        }// end function

        private function updateOtherBuffs(event:Event) : void
        {
            this._buffPanel2.updateEntityBuffer(this._other.entityInfo.buffInfos);
            return;
        }// end function

        override public function stageResize() : void
        {
            super.stageResize();
            this.x = (Global.stage.stageWidth - width) / 2;
            this.y = 10;
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show();
            this.stageResize();
            return;
        }// end function

        private function onTimeChangeHandler() : void
        {
            this._startNum.text = this._sectimeData.leftTime + "";
            if (!this._isDrawMask)
            {
                this._isDrawMask = true;
                if (!this._mask.parent)
                {
                    this._startNum.addChild(this._mask);
                }
                DisplayUtil.drawFullScreenMaskByStageXY(this._mask);
            }
            return;
        }// end function

        private function addNumTween(param1:int) : void
        {
            this._startNum.text = param1 + "";
            this._startNum.scaleX = 2;
            this._startNum.scaleY = 2;
            var _loc_2:* = this._startNum.width * 2;
            var _loc_3:* = this._startNum.height * 2;
            var _loc_4:* = new TimelineLite();
            var _loc_5:* = this._startNum.x;
            var _loc_6:* = this._startNum.y;
            var _loc_7:* = _loc_5 - int((_loc_2 - this._startNum.width) / 2);
            var _loc_8:* = _loc_6 - int((_loc_3 - this._startNum.height) / 2);
            _loc_4.append(new TweenLite(this._startNum, 0.1, {scaleX:2, scaleY:2, x:_loc_7, y:_loc_8}));
            _loc_4.append(new TweenLite(this._startNum, 0.3, {scaleX:1, scaleY:1, x:_loc_5, y:_loc_6}));
            _loc_4.play();
            return;
        }// end function

        private function onTimeComplHandler() : void
        {
            this._startNum.text = "";
            if (this._isDrawMask)
            {
                this._isDrawMask = false;
                DisplayUtil.removeMe(this._mask);
            }
            this.initRoleInfo();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl();
            this._headIcon1.dispose();
            this._headIcon1 = null;
            this._roleLevel1.dispose();
            this._roleLevel1 = null;
            this._roleName1.dispose();
            this._roleName1 = null;
            this._roleCombat1.dispose();
            this._roleCombat1 = null;
            this._bloodBar1.dispose();
            this._bloodBar1 = null;
            this._headIcon2.dispose();
            this._headIcon2 = null;
            this._roleLevel2.dispose();
            this._roleLevel2 = null;
            this._roleName2.dispose();
            this._roleName2 = null;
            this._roleCombat2.dispose();
            this._roleCombat2 = null;
            this._bloodBar2.dispose();
            this._bloodBar2 = null;
            this._buffPanel1.dispose();
            this._buffPanel1 = null;
            this._buffPanel2.dispose();
            this._buffPanel2 = null;
            this._startNum.dispose();
            this._startNum = null;
            this._mask.dispose();
            this._mask = null;
            this._frameTimer = null;
            this._sectimeData.dispose();
            this.removeListeners();
            this._self = null;
            this._other = null;
            return;
        }// end function

        public static function get instance() : AutoArenaVsInfoSpr
        {
            if (_instance == null)
            {
                _instance = new AutoArenaVsInfoSpr;
            }
            return _instance;
        }// end function

    }
}
